package com.party.core.service.sign.impl;

import com.google.common.base.Strings;
import com.party.common.paging.Page;
import com.party.common.utils.DateUtils;
import com.party.common.utils.UUIDUtils;
import com.party.core.dao.read.sign.MemberSignDetailReadDao;
import com.party.core.dao.read.sign.MemberSignReadDao;
import com.party.core.dao.write.sign.MemberSignDetailWriteDao;
import com.party.core.dao.write.sign.MemberSignWriteDao;
import com.party.core.model.BaseModel;
import com.party.core.model.sign.MemberSign;
import com.party.core.model.sign.MemberSignDetail;
import com.party.core.service.sign.IMemberSignService;
import com.sun.istack.NotNull;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.map.HashedMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Set;

/**
 * @Author: huangzequn
 * @Date: 2019/4/16 11:07
 */
@Service
public class MemberSignService implements IMemberSignService {
    @Autowired
    MemberSignReadDao memberSignReadDao;

    @Autowired
    MemberSignWriteDao memberSignWriteDao;

    @Autowired
    MemberSignDetailWriteDao memberSignDetailWriteDao;

    @Autowired
    MemberSignDetailReadDao memberSignDetailReadDao;

    /**
     * @param memberSign
     * @return
     * @Author hzq
     * @Description //TODO
     * @Date 11:19 2019/4/16
     **/
    @Override
    public String insert(MemberSign memberSign) {
        memberSign.setId(UUIDUtils.generateRandomUUID());
        memberSign.setCreateDate(new Date());
        memberSign.setUpdateDate(new Date());
        memberSign.setDelFlag("0");
        memberSign.setLastSignTime(DateUtils.getDateFirstYear(DateUtils.DATETIME_PATTERN));//默认为1970-01-01 00:00:00
        boolean result = memberSignWriteDao.insert(memberSign);
        if (result) {
            return memberSign.getId();
        }
        return null;
    }

    /**
     * @param memberSign
     * @return
     * @Author hzq
     * @Description //TODO
     * @Date 11:19 2019/4/16
     **/
    @Override
    @Transactional
    public boolean update(MemberSign memberSign) {

        MemberSignDetail memberSignDetail = memberSignDetailReadDao.getByDay(memberSign.getId(), new Date());
        if (null == memberSignDetail) {
            memberSignDetail = new MemberSignDetail();
            BaseModel.preInsert(memberSignDetail);
            memberSignDetail.setSignId(memberSign.getId());
            memberSignDetail.setSignTime(DateUtils.getTodayDate());
            memberSignDetailWriteDao.insert(memberSignDetail);
        }
        //获取当前日期和当前机构最后签到时间  相差几天
        int diffDate = DateUtils.diffDate(DateUtils.getTodayDate(),memberSign.getLastSignTime());
        if (diffDate > 1) {
            //说明当前机构连续签到断了
            memberSign.setContinuousTotal(1);
        } else {
            memberSign.setContinuousTotal(memberSign.getContinuousTotal() + 1);
        }
        memberSign.setSignTotal(memberSign.getSignTotal() + 1);
        memberSign.setLastSignTime(DateUtils.getTodayDate());
        return memberSignWriteDao.update(memberSign);
    }

    @Override
    public boolean deleteLogic(@NotNull String id) {
        if (Strings.isNullOrEmpty(id)) {
            return false;
        }
        return memberSignWriteDao.deleteLogic(id);
    }

    @Override
    public boolean delete(@NotNull String id) {
        if (Strings.isNullOrEmpty(id)) {
            return false;
        }
        return memberSignWriteDao.delete(id);
    }

    @Override
    public boolean batchDeleteLogic(Set<String> ids) {
        if (CollectionUtils.isEmpty(ids)) {
            return false;
        }
        return memberSignWriteDao.batchDeleteLogic(ids);
    }

    @Override
    public boolean batchDelete(Set<String> ids) {
        if (CollectionUtils.isEmpty(ids)) {
            return false;
        }
        return memberSignWriteDao.batchDelete(ids);
    }

    @Override
    public MemberSign get(String id) {
        return memberSignReadDao.get(id);
    }

    @Override
    public List<MemberSign> listPage(MemberSign memberSign, Page page) {
        return memberSignReadDao.listPage(memberSign, page);
    }

    @Override
    public List<MemberSign> list(MemberSign memberSign) {
        return memberSignReadDao.listPage(memberSign, null);
    }

    @Override
    public List<MemberSign> batchList(Set<String> ids, MemberSign memberSign, Page page) {
        return memberSignReadDao.batchList(ids, new HashedMap(), page);
    }

    /**
     * @param memberId
     * @return
     * @Author hzq
     * @Description //根据合作商id查询
     * @Date 11:59 2019/4/16
     **/
    @Override
    public MemberSign getByMemberId(String memberId) {
        return memberSignReadDao.getByMemberId(memberId);
    }

    @Override
    public MemberSign getSet(String memberId) {
        MemberSign memberSign = this.getByMemberId(memberId);
        if (null == memberSign) {
            memberSign = new MemberSign();
            memberSign.setMemberId(memberId);
            this.insert(memberSign);
        }
        return memberSign;
    }
}
